package com.chen.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.common.Result;
import com.chen.mapper.ArticleMapper;
import com.chen.mapper.CommentMapper;
import com.chen.mapper.UserMapper;
import com.chen.pojo.Article;
import com.chen.pojo.Comment;
import com.chen.pojo.User;
import com.chen.service.CommentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author chen

 */
@Service
@Transactional
@SuppressWarnings("all")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private ArticleMapper articleMapper;

    public Result findAllComments() {

        List<Comment> comments = commentMapper.selectList(null);
        for (Comment comment : comments) {
            User user = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, comment.getUsername()));
            if (ObjectUtil.isNotEmpty(user)) {
                if (StrUtil.isNotBlank(user.getAvatar())) {
                    comment.setAvatar(user.getAvatar());
                } else {
                    // 默认一个头像
                    comment.setAvatar("https://img2.baidu.com/it/u=2906052251,1830965798&fm=26&fmt=auto");
                }
            }
        }
        return Result.success(comments);
    }


    public Result selectCommentOne(int userId) {

        return Result.success(commentMapper.selectCommentOne(userId));
    }

    public Result insertComment(Comment comment) {
        comment.setDate(new Date());

        return Result.success(commentMapper.insert(comment));
    }

    public Result deleteComment(int id) {


        return Result.success(commentMapper.deleteComment(id));
    }


    public void setUsername() {
        List<Comment> commentList = commentMapper.selectList(null);
        for (Comment comment : commentList) {
            Article selectOne = articleMapper.selectOne(Wrappers.<Article>lambdaQuery().eq(Article::getId, comment.getUserId()));
            if (ObjectUtil.isEmpty(selectOne)) {
                comment.setUsername(selectOne.getAuthor());
            } else {
                comment.setUsername("游客");
            }

        }
    }


    public void setAvatar() {

        List<Comment> comments = commentMapper.selectList(null);
        for (Comment comment : comments) {
            User user = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, comment.getUsername()));
            if (ObjectUtil.isNotEmpty(user)) {
                if (StrUtil.isNotBlank(user.getAvatar())) {
                    comment.setAvatar(user.getAvatar());
                } else {
                    // 默认一个头像
                    comment.setAvatar("https://img2.baidu.com/it/u=2906052251,1830965798&fm=26&fmt=auto");
                }
            }
        }
    }

    public Result updateComment(Comment comment) {

        return Result.success(commentMapper.updateById(comment));
    }


}
